var setting = {
  view: {
    showIcon: false
  },
  edit: {
    drag: {
      autoExpandTrigger: true,
      prev: dropPrev,
      inner: dropInner,
      next: dropNext
    },
    enable: true,
    showRemoveBtn: false,
    showRenameBtn: false
  },
  data: {
    simpleData: {
      enable: true
    }
  },
  callback: {
    beforeDrag: beforeDrag,
    beforeDrop: beforeDrop,
    beforeDragOpen: beforeDragOpen,
    onDrag: onDrag,
    onDrop: onDrop,
    onExpand: onExpand,
    beforeClick: beforeClick,
    onClick: onClick
  }
};

var zNodes = [{
    id: 1,
    pId: 0,
    name: "明确线索信息",
    open: true,
    drag: false,
    childOuter: false
  },
  {
    id: 11,
    pId: 1,
    name: "获取线索信息和客户诉求",
    open: true,
    childOuter: false,
    drag: false
  },
  {
    id: 12,
    pId: 1,
    name: "获取客户计划和预算",
    open: true,
    childOuter: false,
    drag: false
  },

  {
    id: 13,
    pId: 1,
    name: "获取竞争信息",
    open: true,
    childOuter: false,
    drag: false
  },

  {
    id: 2,
    pId: 0,
    name: "研发准备",
    open: true,
    drag: false,
    childOuter: false
  },
  {
    id: 14,
    pId: 2,
    name: "测试用例准备",
    open: true,
    childOuter: false,
    drag: false
  },

  {
    id: 15,
    pId: 2,
    name: "测试用例确认和获取",
    open: true,
    childOuter: false,
    drag: false
  },

  {
    id: 16,
    pId: 2,
    name: "研发内部开发",
    open: true,
    childOuter: false,
    drag: false
  },

];

function dropPrev(treeId, nodes, targetNode) {
  var pNode = targetNode.getParentNode();
  if (pNode && pNode.dropInner === false) {
    return false;
  } else {
    for (var i = 0, l = curDragNodes.length; i < l; i++) {
      var curPNode = curDragNodes[i].getParentNode();
      if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
        return false;
      }
    }
  }
  return true;
}

function dropInner(treeId, nodes, targetNode) {
  if (targetNode && targetNode.dropInner === false) {
    return false;
  } else {
    for (var i = 0, l = curDragNodes.length; i < l; i++) {
      if (!targetNode && curDragNodes[i].dropRoot === false) {
        return false;
      } else if (curDragNodes[i].parentTId && curDragNodes[i].getParentNode() !== targetNode && curDragNodes[i].getParentNode().childOuter === false) {
        return false;
      }
    }
  }
  return true;
}

function dropNext(treeId, nodes, targetNode) {
  var pNode = targetNode.getParentNode();
  if (pNode && pNode.dropInner === false) {
    return false;
  } else {
    for (var i = 0, l = curDragNodes.length; i < l; i++) {
      var curPNode = curDragNodes[i].getParentNode();
      if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
        return false;
      }
    }
  }
  return true;
}

var log, className = "dark",
  curDragNodes, autoExpandNode;

function beforeDrag(treeId, treeNodes) {
  className = (className === "dark" ? "" : "dark");
  // showLog("[ " + getTime() + " beforeDrag ]&nbsp;&nbsp;&nbsp;&nbsp; drag: " + treeNodes.length + " nodes.");
  for (var i = 0, l = treeNodes.length; i < l; i++) {
    if (treeNodes[i].drag === false) {
      curDragNodes = null;
      return false;
    } else if (treeNodes[i].parentTId && treeNodes[i].getParentNode().childDrag === false) {
      curDragNodes = null;
      return false;
    }
  }
  curDragNodes = treeNodes;
  return true;
}

function beforeDragOpen(treeId, treeNode) {
  autoExpandNode = treeNode;
  return true;
}

function beforeDrop(treeId, treeNodes, targetNode, moveType, isCopy) {
  className = (className === "dark" ? "" : "dark");
  // showLog("[ " + getTime() + " beforeDrop ]&nbsp;&nbsp;&nbsp;&nbsp; moveType:" + moveType);
  // showLog("target: " + (targetNode ? targetNode.name : "root") + "  -- is " + (isCopy == null ? "cancel" : isCopy ? "copy" : "move"));
  return true;
}

function onDrag(event, treeId, treeNodes) {
  className = (className === "dark" ? "" : "dark");
  // showLog("[ " + getTime() + " onDrag ]&nbsp;&nbsp;&nbsp;&nbsp; drag: " + treeNodes.length + " nodes.");
}

function onDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
  className = (className === "dark" ? "" : "dark");
  // showLog("[ " + getTime() + " onDrop ]&nbsp;&nbsp;&nbsp;&nbsp; moveType:" + moveType);
  // showLog("target: " + (targetNode ? targetNode.name : "root") + "  -- is " + (isCopy == null ? "cancel" : isCopy ? "copy" : "move"))
}

function onExpand(event, treeId, treeNode) {
  if (treeNode === autoExpandNode) {
    className = (className === "dark" ? "" : "dark");
    showLog("[ " + getTime() + " onExpand ]&nbsp;&nbsp;&nbsp;&nbsp;" + treeNode.name);
  }
}
var log, className = "dark";

function beforeClick(treeId, treeNode, clickFlag) {
  className = (className === "dark" ? "" : "dark");
  showLog(treeNode.name);
  return (treeNode.click != false);
}

function onClick(event, treeId, treeNode, clickFlag) {
  var sNodes = $.fn.zTree.getZTreeObj("treeDemo").getSelectedNodes();
  var penNode, penName, chilName;
  if (sNodes.length > 0) {
    var node = sNodes[0].getParentNode();
    // console.log(node);
    if (sNodes[0].pId == null || sNodes[0].pId == "" || sNodes[0].pId == undefined) {
      layer.msg("请选择一个任务节点或子任务节点");
      return;
    }
    if (sNodes[0].pId != null || sNodes[0].pId != "" || sNodes[0].pId != undefined) {
      penNode = node.getParentNode();
      if (penNode != null) {
        penNode = penNode.name;
      }
      penName = node.name;
      chilName = sNodes[0].name;
      showLog(penNode, penName, chilName)
    }
  }
}

function showLog(penNode, penName, chilName) {
  if (penNode != null) {
    $("#penNode").val(penNode);
    $("#penName").val(penName);
    $("#chilName").val(chilName);
  } else {
    $("#penNode").val(penName);
    $("#penName").val(chilName);
  }
}

function getTime() {
  var now = new Date(),
    h = now.getHours(),
    m = now.getMinutes(),
    s = now.getSeconds(),
    ms = now.getMilliseconds();
  return (h + ":" + m + ":" + s + " " + ms);
}
var newCount = 1;

function add1(e) {
  var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
    isParent = e.data.isParent,
    nodes = zTree.getSelectedNodes(),
    treeNode = nodes[0];
  var sNodes = $.fn.zTree.getZTreeObj("treeDemo").getSelectedNodes();
  if (sNodes.length > 0) {
    var node = sNodes[0].getParentNode();
    if (sNodes[0].pId == null || sNodes[0].pId == "" || sNodes[0].pId == undefined) {
      layer.msg("请先选择一个任务节点再进行新增");
      return;
    }
    if (sNodes[0].childOuter == false) {
      var val = $("#penName").val();
      var chilName = $("#chilName").val();
      var krqz = $("#krqz").val();
      var fzr = $("#fzr").val();
      var overtime = $("#overtime").val();
      var okrt = $("#okrt").val();
      if (chilName == null || chilName == "" || chilName == undefined) {
        layer.msg("子任务不能为空！");
        return;
      }
      if (krqz == null || krqz == "" || krqz == undefined) {
        layer.msg("KR权重不能为空！");
        return;
      }
      if (fzr == null || fzr == "" || fzr == undefined) {
        layer.msg("负责人不能为空！");
        return;
      }
      if (overtime == null || overtime == "" || overtime == undefined) {
        layer.msg("完成时间不能为空！");
        return;
      }
      if (okrt == null || okrt == "" || okrt == undefined) {
        layer.msg("投入百分比不能为空！");
        return;
      } else {
        treeNode = zTree.addNodes(treeNode, {
          id: (100 + newCount),
          pId: treeNode.id,
          isParent: isParent,
          name: chilName
        });
        layer.msg("新增成功");
      }
    } else {
      layer.msg("请先选择一个任务节点再进行新增");
      return;
    }
  }
}

function edit(e) {
  var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
    isParent = false,
    nodes = zTree.getSelectedNodes(),
    treeNode = nodes[0];
  var sNodes = $.fn.zTree.getZTreeObj("treeDemo").getSelectedNodes();
  if (sNodes.length > 0) {
    var node = sNodes[0].getParentNode();
    if (sNodes[0].pId == null || sNodes[0].pId == "" || sNodes[0].pId == undefined) {
      layer.msg("请先选择一个子任务节点再进行更新");
      return;
    }
    if (sNodes[0].childOuter == false) {
      layer.msg("请先选择一个子任务节点再进行更新");
      return;
    } else {
      var val = $("#chilName").val();
      var krqz = $("#krqz").val();
      var fzr = $("#fzr").val();
      var overtime = $("#overtime").val();
      var okrt = $("#okrt").val();
      if (val == null || val == "" || val == undefined) {
        layer.msg("子任务不能为空！");
        return;
      }
      if (krqz == null || krqz == "" || krqz == undefined) {
        layer.msg("KR权重不能为空！");
        return;
      }
      if (fzr == null || fzr == "" || fzr == undefined) {
        layer.msg("负责人不能为空！");
        return;
      }
      if (overtime == null || overtime == "" || overtime == undefined) {
        layer.msg("完成时间不能为空！");
        return;
      }
      if (okrt == null || okrt == "" || okrt == undefined) {
        layer.msg("投入百分比不能为空！");
        return;
      } else {
        treeNode.name = val;
        treeNode = zTree.updateNode(treeNode);
        layer.msg("更新成功");
      }
    }
  }
}

function remove(e) {
  var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
    nodes = zTree.getSelectedNodes(),
    treeNode = nodes[0];
  var sNodes = $.fn.zTree.getZTreeObj("treeDemo").getSelectedNodes();
  if (sNodes.length > 0) {
    var node = sNodes[0].getParentNode();
    if (sNodes[0].pId == null || sNodes[0].pId == "" || sNodes[0].pId == undefined) {
      layer.msg("请先选择一个子任务节点再进行删除");
      return;
    }
    if (sNodes[0].childOuter == false) {
      layer.msg("请先选择一个子任务节点再进行删除");
      return;
    } else {
      var callbackFlag = $("#callbackTrigger").attr("checked");
      layer.confirm('确认删除子任务节点？', {
        btn: ['确认', '取消'] //按钮
      }, function () {
        // mmg.updateRow(item1, 0);
        zTree.removeNode(treeNode, callbackFlag);
        $("#chilName").val("");
        $("#krqz").val("");
        $("#fzr").val("");
        $("#overtime").val("");
        $("#okrt").val("");
        layer.msg('删除成功', {
          icon: 1
        });
      });
    }
  }
}

function setTrigger() {
  var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  zTree.setting.edit.drag.autoExpandTrigger = $("#callbackTrigger").attr("checked");
}

$(document).ready(function () {
  $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  $("#callbackTrigger").bind("change", {}, setTrigger);
  $("#addLeaf").bind("click", {
    isParent: false
  }, add1);
  $("#remove").bind("click", remove);
  $("#edit").bind("click", edit);
});

//时间控件
$('#overtime').datepicker({
  format: 'yyyy-mm-dd'
});



// 项目公告管理
var plistColumns = [{
  id: 'title',
  title: '项目目标',
  type: 'string',
  width: 100,
  columnClass: 'text-center',
}, {
  id: 'test',
  title: '关键成果',
  type: 'string',
  width: 100,
  columnClass: 'text-left',
}, {
  id: 'name',
  title: '计划完成时间',
  type: 'string',
  width: 100,
  columnClass: 'text-center',
}, {
  id: 'time',
  title: 'KR权重',
  type: 'string',
  width: 70,
  columnClass: 'text-center',
}, {
  id: 'username',
  title: '成员',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}];
var plistOption = {
  lang: 'zh-cn',
  ajaxLoad: false,
  check : true,
  checkWidth : 20,
  // loadURL:'url',
  exportFileName: '项目公告',
  datas: [{
    id: 1,
    title: '测试数据',
    test: '测试数据',
    name: '2018-06-12',
    time: '20%',
    username: '测试数据',
  }, {
    id: 2,
    title: '测试数据',
    test: '测试数据',
    name: '2018-06-12',
    time: '20%',
    username: '测试数据',
  }, {
    id: 3,
    title: '测试数据',
    test: '测试数据',
    name: '2018-06-12',
    time: '20%',
    username: '测试数据',
  }, {
    id: 4,
    title: '测试数据',
    test: '测试数据',
    name: '2018-06-12',
    time: '20%',
    username: '测试数据',
  },{
    id: 5,
    title: '测试数据',
    test: '测试数据',
    name: '2018-06-12',
    time: '20%',
    username: '测试数据',
  }, {
    id: 6,
    title: '测试数据',
    test: '测试数据',
    name: '2018-06-12',
    time: '20%',
    username: '测试数据',
  }, {
    id: 7,
    title: '测试数据',
    test: '测试数据',
    name: '2018-06-12',
    time: '20%',
    username: '测试数据',
  }, {
    id: 8,
    title: '测试数据',
    test: '测试数据',
    name: '2018-06-12',
    time: '20%',
    username: '测试数据',
  }, {
    id: 9,
    title: '测试数据',
    test: '测试数据',
    name: '2018-06-12',
    time: '20%',
    username: '测试数据',
  }, {
    id: 10,
    title: '测试数据',
    test: '测试数据',
    name: '2018-06-12',
    time: '20%',
    username: '测试数据',
  },  ],
  columns: plistColumns,
  gridContainer: 'plistContainer',
  toolbarContainer: 'plistToolBarContainer',
  tools: '',
  pageSize: 10,
  pageSizeLimit: [10, 15, 20]
};
var plistGrid = $.fn.dlshouwen.grid.init(plistOption);
$(function () {
  plistGrid.load();
});


function plistGridCheckedRecordsLength(){
	var recordCount = plistGrid.getCheckedRecords().length;
	if(recordCount <= 0){
    layer.msg("请至少勾选一条数据！");
  }else{
    console.log(recordCount); 
  }
}